package org.apache.cordova.baidumap;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;
import android.content.Context;
import android.util.Log;

import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.SDKInitializer;

public class BaiduSdkPlugin extends CordovaPlugin {

    public static final String TAG = "org.apache.cordova.baidumap";

    @Override
    public boolean execute(final String action, final JSONArray data, final CallbackContext callbackContext) throws JSONException {
        if (TextUtils.isEmpty(action)) {
            callbackContext.error("method[" + action + "] isEmpty!");
            return false;
        }

        final Context context = cordova.getActivity().getApplicationContext();
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    Method method = BaiduSdkPlugin.class.getDeclaredMethod(action, JSONArray.class, CallbackContext.class, Context.class);
                    method.invoke(BaiduSdkPlugin.this, data, callbackContext, context);
                    Log.v(TAG, action + ": " + data.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                    callbackContext.error(e.getMessage());
                }
            }
        };

        //if ("initialize".equals(action)) {
            cordova.getActivity().runOnUiThread(runnable);
        //} else {
        //    cordova.getThreadPool().execute(runnable);
        //}
        return true;
    }

    void initialize(JSONArray data, CallbackContext callbackContext, Context context) {
        try {
            SDKInitializer.initialize(context);
            callbackContext.success();
        } catch (Exception e) {
            e.printStackTrace();
            callbackContext.error(e.getMessage());
        }
    }

    void createMap(JSONArray data, CallbackContext callbackContext, Context context) {
        try {

            MapView mapView = new MapView(webView.getContext());
            /*
            MapController mapController = mapView.getController();
            GeoPoint point = new GeoPoint((int) (mLat * 1E6), (int) (mLng * 1E6));
            mapController.setCenter(point);
            mapController.setZoom(mZoom);

            float scale = BaiduMap.this.webView.getScale();

            LayoutParams params = new LayoutParams((int) (mWidth * scale),
                    (int) (mHeight * scale), (int) (mLeft * scale),
                    (int) (mTop * scale));
            mapView.setLayoutParams(params);
            */
            webView.addView(mapView);

            callbackContext.success();
        } catch (Exception e) {
            e.printStackTrace();
            callbackContext.error(e.getMessage());
        }
    }

}
